//add by seonghankim

package com.lge.remoteshot.camera;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class FoucsView extends View{
	public final int maxCount = 7;
	static int downX, downY;
	float focusX, focusY;
	Paint paint;
	//int radius = 30;
	int count = maxCount;
	boolean bNowFocusing = false;
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			
			count --;
			invalidate();
			if(count <= 0){
				bNowFocusing = false;
				count = maxCount;
			}
			else
				mHandler.sendEmptyMessageDelayed(0, 100);
		}};
	
	public FoucsView(Context context){
		super(context);
		paint = new Paint();
	}
	public FoucsView(Context context, AttributeSet set){
		super(context, set);
		paint = new Paint();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		//super.onDraw(canvas);
		if(count != maxCount){
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeWidth(1);
			paint.setColor(Color.GREEN);
			
			
			focusX = (float)downX / (float)getWidth();
			focusY = (float)downY / (float)getHeight();

			int padding = getWidth()/10 + count*3;
			canvas.drawRect(new Rect(downX-(padding+3), downY-(padding+3), downX+(padding+3), downY+(padding+3)), paint);
			canvas.drawRect(new Rect(downX-padding, downY-padding, downX+padding, downY+padding), paint);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event){
		int action = event.getAction();
		
		switch(action){
		case MotionEvent.ACTION_UP:
			Log.v("TAG", "ACTION_UP");
			break;
		case MotionEvent.ACTION_MOVE:
			Log.v("TAG", "ACTION_MOVE");
			break;
		case MotionEvent.ACTION_DOWN:
			if(!bNowFocusing){
				bNowFocusing = true;
				downX = (int)event.getX();
				downY = (int)event.getY();
				mHandler.sendEmptyMessageDelayed(0, 0);
				return false;
			}
			break;
		}
		return true;
	}
	
	public float getFoucsX(){
		return focusX;
	}
	public float getFoucsY(){
		return focusY;
	}
	
}